package cn.itcast.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Menu;
import cn.itcast.service.MenuService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@RestController
@RequestMapping("/menu")
@Slf4j
public class MenuController {

    @Reference
    private MenuService menuService;


    /**
     * 分页查菜单
     *
     * @param queryPageBean
     * @return
     */
    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    @GetMapping("/page")
    public Result queryMenu(QueryPageBean queryPageBean) {
        try {
            log.debug("分页查询传来的参数:{}", queryPageBean);

            PageResult pageResult = menuService.queryMenu(queryPageBean);
            if (ObjectUtil.isNotEmpty(pageResult)) {
                return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, pageResult);
            }
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL,e);
        }
    }

    /**
     * 新增菜单
     *
     * @param permissionIds
     * @param menu
     * @return
     */
    @PreAuthorize("hasAnyAuthority('MENU_ADD')")
    @PostMapping
    public Result addMenu(Integer[] permissionIds, @RequestBody Menu menu) {
        try {
            log.debug("新增修改的权限id上:{}", permissionIds);
            log.debug("新增菜单的信息:{}", menu);

            Boolean bool  = menuService.addMenuAndOrder(permissionIds, menu);

            if (bool) {
                return new Result(bool, MessageConstant.ADD_MENU_SUCCESS);
            }
            return new Result(bool, MessageConstant.ADD_MENU_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL,e);
        }
    }


    /**
     * 编辑回显菜单的数据
     *
     * @param mid
     * @return
     */
    @GetMapping("/{id}")
    public Result queryMenuById(@PathVariable("id") Integer mid) {
        try {
            log.debug("回显数据的id：{}", mid);
            Menu menu = menuService.queryMenuById(mid);

            if (ObjectUtil.isNotEmpty(menu)) {
                return new Result(true, MessageConstant.EDIT_MENU_SUCCESS, menu);
            }
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL,e);
        }
    }

    /**
     * 修改菜单
     *
     * @param permissionIds
     * @param menu
     * @return
     */
    @PreAuthorize("hasAnyAuthority('MENU_EDIT')")
    @PutMapping
    public Result updateMenu(Integer[] permissionIds, @RequestBody Menu menu) {
        try {
            log.debug("修改的权限id上:{}", permissionIds);
            log.debug("修改菜单的信息:{}", menu);
            Boolean bool  = menuService.updateMenu(permissionIds, menu);
            if (bool) {
                return new Result(bool, MessageConstant.EDIT_MENU_SUCCESS);
            }
            return new Result(bool, MessageConstant.EDIT_MENU_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL,e);
        }
    }

    /**
     * 删除菜单
     *
     * @param mid
     * @return
     */
    @PreAuthorize("hasAnyAuthority('MENU_DELETE')")
    @DeleteMapping("/{id}")
    public Result deleteMenuById(@PathVariable("id") Integer mid) {
        try {
            log.debug("删除的菜单的id:{}", mid);
            Boolean bool = menuService.deleteMenuById(mid);
            if (bool) {
                return new Result(bool, MessageConstant.DELETE_MENU_SUCCESS);
            }
            return new Result(bool, MessageConstant.DELETE_MENU_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MENU_FAIL,e);
        }
    }

    /**
     * 根据username查询用户对应的菜单
     * @return
     */
    @GetMapping("/queryMenuList")
    public Result queryMenuListByUserName(){
        try {
            //通过SpringSecurity的域对象获取
            String username =  SecurityContextHolder.getContext().getAuthentication().getName();
            log.debug("需要查询的username:{}",username);
            Set<Menu> menuList= menuService.queryMenuListByUserName(username);
            if (CollUtil.isNotEmpty(menuList)){
                return new Result(true,MessageConstant.GET_MENU_SUCCESS,menuList);
            }
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.GET_MENU_FAIL,e);
        }
    }
}
